import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../providers/provider_container.dart';
import '../entity/room_bean.dart';
import '../intent/RoomListState.dart';
import '../model/room_list_viewmodel.dart';

class RoomListPage extends ConsumerStatefulWidget {
  final String title;

  RoomListPage({required this.title});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() {
    return _RoomListPageState();
  }
}

class _RoomListPageState extends ConsumerState<RoomListPage> {
  RoomListViewModel get viewModel => ref.read(roomListViewModelProvider.notifier);

  @override
  void initState() {
    super.initState();
    // 在页面初始化时执行网络请求
    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.initData();
    });
  }

  @override
  Widget build(BuildContext context) {
    final RoomListState state = ref.watch(roomListViewModelProvider);
    return Scaffold(
      appBar: AppBar(title: Text(widget.title), centerTitle: true),
      body: SafeArea(
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            RoomBean item = state.roomList[index];
            return InkWell(
              onTap: () {
                viewModel.goNextPage(item);
              },
              child: Container(
                child: Column(
                  children: [
                    SizedBox(height: 10),
                    Text("电影${index + 1}房"),
                    SizedBox(height: 10),
                    Divider(thickness: 1, indent: 10, endIndent: 10, color: Colors.grey),
                  ],
                ),
              ),
            );
          },
          itemCount: state.roomList.length,
        ),
      ),
    );
  }
}
